package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.bind.v2.model.core.ID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节的service
    @Autowired
    private EduVideoService videoService;

    //注入章节的service
    @Autowired
    private EduChapterService chapterService;

    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1、向课程表添加课程基本信息
        //courseInfoVo对象转换为eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);//BeanUtils会自动找相同属性注入
        int insert = baseMapper.insert(eduCourse);
        if(insert<=0){
            throw new GuliException(20001,"添加课程信息失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();

        //2、向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id（设置描述id为input）
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        //返回课程id
        return cid;
    }

    //根据课程id查询课程的基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1、根据课程id查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //把课程表数据放到（courseInfoVo：一级分类）
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2、查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        //把描述表的简介给（courseInfoVo：一级分类）
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }


    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1、修改课程表
        //创建课程对象，把传进来的（courseInfoVo：一级分类）放到课程对象
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        //执行数据库修改
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2、修改描述表
        //创建描述对象，根据传进来的（courseInfoVo：一级分类）的id把简介，放到描述对象
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());

        //执行数据库修改
        courseDescriptionService.updateById(description);
    }



    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String courseId) {
        //调用mapper
        CoursePublishVo publishCpurseInfo = baseMapper.getPublishCpurseInfo(courseId);
        return publishCpurseInfo;
    }


    //删除课程(删除：小节、章节、描述、课程本身)
    @Override
    public void removeCourse(String courseId) {
        //1、根据课程id删除 小节，同时删除对应的阿里云视频
        videoService.removeVideoByCourseId(courseId);

        //2、根据课程id删除 章节
        chapterService.removeChapterByCourseId(courseId);

        //3、根据课程id删除 描述
        courseDescriptionService.removeById(courseId);

        //4、根据课程id删除 课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0){
            throw new GuliException(20001,"课程删除失败");
        }
    }


    //1、条件查询带分页查询课程
    @Override
    public HashMap<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //判断条件是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){ //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());//查询与点击的值相同的一级分类
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){ //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());//查询与点击的值相同的二级分类
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){ //销量排序
            wrapper.orderByDesc("buy_count",courseFrontVo.getBuyCountSort());//降序排列所有的销量
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {//最新时间排序
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格排序
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);

        List<EduCourse> records = pageCourse.getRecords();//每页数据集合
        long current = pageCourse.getCurrent();//当前页
        long pages = pageCourse.getPages();//总页数
        long size = pageCourse.getSize();//每页记录数
        long total = pageCourse.getTotal();//总记录数
        boolean hasNext = pageCourse.hasNext();//是否有下一页
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页

        //把分页集合获取出来，放到map集合
        HashMap<String, Object> map=new HashMap<>();
        map.put("items",records);//每页数据集合
        map.put("current",current);//当前页
        map.put("pages",pages);//总页数
        map.put("size",size);//每页记录数
        map.put("total",total);//总记录数
        map.put("hasNext",hasNext);//是否有下一页
        map.put("hasPrevious",hasPrevious);//是否有上一页

        //map返回
        return map;
    }


    //1、根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }

}
